package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.DruPurchase;
import com.ruoyi.project.drug.domain.DruPurchaseDrug;
import com.ruoyi.project.drug.service.DruPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@Transactional
@RequestMapping("/drug/purchase")
public class DruPurchaseController extends BaseController {


    @Autowired
    private DruPurchaseService druPurchaseService;



    /**
     * 根据条件查询采购订单信息
     *
     * @param druPurchase  传递采购订单查询条件参数
     * @return  返回查询结果集合
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:purchase:selectAll')")
    @GetMapping("/selectAll")
    public TableDataInfo selectAll(DruPurchase druPurchase) {

        startPage();

        List<DruPurchase> list = druPurchaseService.selectAll(druPurchase);

        return getDataTable(list);
    }


    /**
     * 根据采购单号查询采购单详情信息
     *
     * @param dpid  采购单号
     * @return  返回携带采购订单信息和采购的药品信息集合
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:purchase:selectAll')")
    @GetMapping("/selectParticulars/{dpid}")
    public AjaxResult selectParticulars(@PathVariable String dpid) {

        Map<String, Object> map = druPurchaseService.selectParticulars(dpid);

        return AjaxResult.success(map);
    }


    /**
     * 修改采购单信息
     *
     * @param druPurchase  采购单数据
     * @return 成功返回1
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:purchase:update')")
    @PutMapping("/updatepurchase")
    public AjaxResult updatepurchase(@RequestBody DruPurchase druPurchase) {

        druPurchase.setUpdateBy(SecurityUtils.getUsername());
        druPurchase.setUpdateTime(new Date());

        return toAjax(druPurchaseService.updatepurchase(druPurchase));
    }


    /**
     * 提交审核
     *
     * @param dpids  订单号
     * @return  成功返回1
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:purchase:update')")
    @PostMapping("/submitAudit/{dpids}/{state}")
    public AjaxResult submitAudit(@PathVariable String[] dpids, @PathVariable String state) {
        return toAjax(druPurchaseService.submitAudit(dpids,state));
    }



    /**
     * 根据订单号查询信息
     *
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:purchase:delete')")
    @DeleteMapping("/deletePurchase/{dpids}")
    public AjaxResult deletePurchase(@PathVariable String[] dpids) {
        return toAjax(druPurchaseService.deletePurchase(dpids));
    }
}
